package com.bisnu.datetime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DateTimeActivity extends Activity {
	
	Button btndate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btndate=(Button)findViewById(R.id.Button01);
        Thread myThread = null;



        Runnable runnable = new CountDownRunner();
       myThread= new Thread(runnable);   
        myThread.start();

        btndate.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				//String currentDateTimeString = DateFormat.getDateInstance().format(new Date());

				String date=getDateTime();
				Toast.makeText(DateTimeActivity.this, "Current date: "+date, Toast.LENGTH_LONG).show();
				
			}
		});
    }
    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    class CountDownRunner implements Runnable{
        // @Override
        public void run() {
                while(!Thread.currentThread().isInterrupted()){
                    try {
                    doWork();
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                    }catch(Exception e){
                    }
                }
        }

		private void doWork() {
			runOnUiThread(new Runnable() {
			    public void run() {
			        try{
			    TextView txtCurrentTime= (TextView)findViewById(R.id.TextView01);
			                Date dt = new Date();
			                int hours = dt.getHours();
			                int minutes = dt.getMinutes();
			                int seconds = dt.getSeconds();
			                String curTime = hours + ":"+minutes + ":"+ seconds;
			                txtCurrentTime.setText(curTime);
			    }catch (Exception e) {

			    }
			    }
			});

			
		}
    }

}